package com.example.emos.wx.db.mapper;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.example.emos.wx.db.pojo.MessageEntity;
import com.example.emos.wx.db.pojo.MessageRefEntity;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName MessageDao
 * @Description TODO
 * @Author luowenhong
 * @Version 1.0
 **/

@Repository
@Slf4j
public class MessageDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    public String insert(MessageEntity message){
        /** 把北京时间转换成格林尼治时间 */
        Date sendTime = message.getSendTime();
        sendTime = DateUtil.offset(sendTime, DateField.HOUR, 8);
        message.setSendTime(sendTime);
        message = mongoTemplate.save(message);
        return message.get_id();
    }

    public HashMap searchMessageById(String id){
        HashMap map = mongoTemplate.findById(id, HashMap.class, "message");
        Date sendTime = (Date) map.get("sendTime");
        // 把格林尼治时间转换成北京时间
        sendTime = DateUtil.date(sendTime).offset(DateField.HOUR, -8);
        map.replace("sendTime", DateUtil.format(sendTime, "yyyy-MM-dd HH:mm"));
        return map;
    }

    /**
     * 分页查询指定用户（未读、已读）消息
     * @author luowenhong
     * @param
     */
    public List<HashMap> searchMessageByPage(Integer receiverId, long start, int length){
        JSONObject json = new JSONObject();
        json.set("$toString", "$_id");
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.addFields().addField("id").withValue(json).build(),
                Aggregation.lookup("message_ref", "id", "messageId", "ref"),
                Aggregation.match(Criteria.where("ref.receiverId").is(receiverId)),
                Aggregation.project("id", "msg", "sendTime","senderId","senderName","senderPhoto","uuid")
                        .and(item-> Document.parse("{$filter:{input:\"$ref\",as:\"item\",cond:{$eq:[\"$$item.receiverId\","+receiverId+"]}}}")).as("ref"),
                Aggregation.sort(Sort.by(Sort.Direction.DESC, "sendTime")),
                Aggregation.skip(start),
                Aggregation.limit(length)
        );
        AggregationResults<HashMap> results = mongoTemplate.aggregate(aggregation, "message", HashMap.class);
        // 加工数据
        List<HashMap> list = results.getMappedResults();
        list.forEach(one -> {
            List<MessageRefEntity> refList = (List<MessageRefEntity>) one.get("ref");
            MessageRefEntity entity = refList.get(0);
            boolean readFlag = entity.getReadFlag();
            String refId = entity.get_id();
            one.remove("ref");
            one.remove("_id");
            one.put("readFlag", readFlag);
            one.put("refId", refId);
            //把格林尼治时间转换成北京时间
            Date sendTime = (Date) one.get("sendTime");
            sendTime = DateUtil.offset(sendTime, DateField.HOUR, -8);

            String today = DateUtil.today();
            //如果是今天的消息，只显示发送时间，不需要显示日期
            if (today.equals(DateUtil.date(sendTime).toDateStr())) {
                one.put("sendTime", DateUtil.format(sendTime, "HH:mm"));
            }
            //如果是以往的消息，只显示日期，不显示发送时间
            else {
                one.put("sendTime", DateUtil.format(sendTime, "yyyy/MM/dd"));
            }
        });


        return list;
    }

    public long deleteUserMessage(int receiverId){
        Query query=new Query();
        query.addCriteria(Criteria.where("receiverId").is(receiverId));
        DeleteResult result=mongoTemplate.remove(query,"message");
        long rows=result.getDeletedCount();
        return rows;
    }

}
